﻿using mozhi.smarterp.Business.Purchases.Repositories;
using Mozhi.Abp.Domain.BasicData;
using Mozhi.Abp.Domain.BasicData.Repositories;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Domain.Services;
using Volo.Abp.Guids;

namespace mozhi.smarterp.Business.Purchases.Services
{
    public class PurchaseProductPriceManager : DomainService
    {
        private ISupplierRepository _supplierRepository;
        private IProductRepository _productRepository;
        private ICurrencyRepository _currencyRepository;
        private IPurchaseProductPriceRepository _repository;
        private ITaxRateRepository _taxRateRepository;
        private IGuidGenerator _guidGenerator;

        public PurchaseProductPriceManager(ISupplierRepository supplierRepository, IProductRepository productRepository, IPurchaseProductPriceRepository repository,
            ICurrencyRepository currencyRepository, IGuidGenerator guidGenerator, ITaxRateRepository taxRateRepository)
        {
            _supplierRepository = supplierRepository;
            _productRepository = productRepository;
            _repository = repository;
            _guidGenerator = guidGenerator;
            _currencyRepository = currencyRepository;
            _taxRateRepository = taxRateRepository;
        }

        public async Task<PurchaseProductPrice> GetAsync(Guid? id, string code, string name, Guid? supplierId, Guid currencyId, bool includeTax)
        {
            var count = await _repository.GetCountAsync(null, code, null, null, null, id) + await _repository.GetCountAsync(null, null, name, null, null, id);

            if (count > 0)
                throw new UserFriendlyException($"价目表代码或者名称已经存在:{code}-{name}", "100000");

            PurchaseProductPrice productPrice;

            var supplier = supplierId.HasValue ? await _supplierRepository.GetAsync(supplierId.Value) : null;

            var currency = await _currencyRepository.GetAsync(currencyId);

            if (id.HasValue)
            {
                productPrice = await _repository.GetAsync(id.Value);

                productPrice.Change(code, name, supplier, currency, includeTax);
            }
            else
                productPrice = new PurchaseProductPrice(_guidGenerator.Create(), code, name, supplier, currency, includeTax);

            return productPrice;
        }

        public async Task<PurchaseProductPriceItem> GetItemAsync(Guid? id, Guid productId, decimal price, decimal? minQuantity, decimal? maxQuantity, Guid? taxRateId,
            DateTime effectiveDate, DateTime expirationDate)
        {
            var product = await _productRepository.GetAsync(productId);
            var taxRate = taxRateId.HasValue ? await _taxRateRepository.GetAsync(taxRateId.Value) : null;

            id = id ?? _guidGenerator.Create();


            return new PurchaseProductPriceItem(id.Value, product, price, minQuantity, maxQuantity, taxRate, effectiveDate, expirationDate);
        }
    }
}
